<template>
  <div class="updatePerson">
    <h3 class="title">修改人事信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="name">
        <el-input disabled v-model="ruleForm.item.name"></el-input>
      </el-form-item>
      <el-form-item label="工号	" prop="jobNumber">
        <el-input v-model="ruleForm.item.jobNumber"></el-input>
      </el-form-item>
      <el-form-item label="账号密码" prop="passWord">
        <el-input type="password" autocomplete="off" v-model="ruleForm.item.passWord"></el-input>
      </el-form-item>
      <el-form-item label="个人电话" prop="phone">
        <el-input v-model="ruleForm.item.phone"></el-input>
      </el-form-item>
      <el-form-item label="住址" prop="address">
        <el-input v-model="ruleForm.item.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button type="info" :plain="true" @click="close()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminRequest from "../request/admin";
export default {
  props: ["item", "clearComp"],
  data() {
    var name = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写个人名称"));
      } else {
        return callback();
      }
    };
    var jobNumber = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写工号"));
      } else {
        return callback();
      }
    };
    var passWord = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写员工密码"));
      } else {
        return callback();
      }
    };
    var phone = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写个人电话"));
      } else {
        return callback();
      }
    };

    var address = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写住址"));
      } else {
        return callback();
      }
    };

    return {
      ruleForm: {
        item: this.item,
      },
      rules: {
        name: [{ validator: name, trigger: "blur" }],
        passWord: [{ validator: passWord, trigger: "blur" }],
        phone: [{ validator: phone, trigger: "blur" }],
        jobNumber: [{ validator: jobNumber, trigger: "blur" }],
        address: [{ validator: address, trigger: "blur" }],
      },
    };
  },

  methods: {
    close() {
      this.clearComp();
    },
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          adminRequest
            .addPersonnel({
              id: this.item.id,
              name: this.ruleForm.item.name,
              passWord: this.ruleForm.item.passWord,
              phone: this.ruleForm.item.phone,
              address: this.ruleForm.item.address,
              jobNumber: this.ruleForm.item.jobNumber,
              createTime: this.item.createTime,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("修改成功!");
                this.clearComp();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
  },

  created() {
    console.log(this.item);
  },
};
</script>

<style lang="scss" scoped>
.updatePerson {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 10px;
    height: 50px;
    display: flex;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }
}
</style>